~ chicken-core (master) /manual/Module (chicken tcp)
Trap1[[tags: manual]]
2[[toc:]]
3
4== Module (chicken tcp)
5
6This module provides basic facilities for communicating over TCP
7sockets.
8
9All errors related to failing network operations will raise a condition
10of kind {{(exn i/o net)}}.
11
12
13=== tcp-listen
14
15<procedure>(tcp-listen TCPPORT [BACKLOG [HOST]])</procedure>
16
17Creates and returns a TCP listener object that listens for connections on {{TCPPORT}}, which
18should be an exact integer. {{BACKLOG}} specifies the number of maximally pending
19connections (and defaults to 100). If the optional argument {{HOST}} is given and not
20{{#f}}, then only incoming connections for the given host (or IP) are accepted.
21
22
23=== tcp-listener?
24
25<procedure>(tcp-listener? X)</procedure>
26
27Returns {{#t}} if {{X}} is a TCP listener object, or {{#f}} otherwise.
28
29
30=== tcp-close
31
32<procedure>(tcp-close LISTENER)</procedure>
33
34Reclaims any resources associated with {{LISTENER}}.
35
36
37=== tcp-accept
38
39<procedure>(tcp-accept LISTENER [ENCODING])</procedure>
40
41Waits until a connection is established on the port on which
42{{LISTENER}} is listening and returns two values: an input- and
43output-port that can be used to communicate with the remote
44process. The current value of {{tcp-accept-timeout}} is used to
45determine the maximal number of milliseconds (if any) to wait
46until a connection is established. When a client connects any
47read- and write-operations on the returned ports will use the
48current values (at the time of the connection) of {{tcp-read-timeout}}
49and {{tcp-write-timeout}}, respectively, to determine the maximal
50number of milliseconds to wait for input/output before a timeout
51error is signalled.
52
53{{ENCODING}} should be a symbol specifying the encoding to be
54used for I/O operations, the default is {{utf-8}}. The encodings for
55the returned in and output ports can be subsequently changed by
56using the {{port-encoding}} setter.
57
58Note: this operation and any I/O on the ports returned will not block
59other running threads.
60
61
62=== tcp-accept-ready?
63
64<procedure>(tcp-accept-ready? LISTENER)</procedure>
65
66Returns {{#t}} if there are any connections pending on {{LISTENER}}, or {{#f}}
67otherwise.
68
69
70=== tcp-listener-port
71
72<procedure>(tcp-listener-port LISTENER)</procedure>
73
74Returns the port number assigned to {{LISTENER}} (If you pass {{0}} to {{tcp-listen}},
75then the system will choose a port-number for you).
76
77=== tcp-listener-fileno
78
79<procedure>(tcp-listener-fileno LISTENER)</procedure>
80
81Returns the file-descriptor associated with {{LISTENER}}.
82
83
84=== tcp-connect
85
86<procedure>(tcp-connect HOSTNAME [TCPPORT ENCODING])</procedure>
87
88Establishes a client-side TCP connection to the machine with the name
89{{HOSTNAME}} (a string) at {{TCPPORT}} (an exact integer) and returns
90two values: an input- and output-port for communicating with the
91remote process. The current value of {{tcp-connect-timeout}} is used
92to determine the maximal number of milliseconds (if any) to wait until
93the connection is established. When the connection takes place any
94read- and write-operations on the returned ports will use the current
95values (at the time of the call to {{tcp-connect}}) of {{tcp-read-timeout}} and
96{{tcp-write-timeout}}, respectively, to determine the maximal number
97of milliseconds to wait for input/output before a timeout error is
98signalled.
99
100If the {{TCPPORT}} is omitted, the port is parsed from the {{HOSTNAME}} string. The format expected is {{HOSTNAME:PORT}}. The {{PORT}} can either be a string representation of an integer or a service name which is translated to an integer using the POSIX function [[http://www.opengroup.org/onlinepubs/009695399/functions/getservbyname.html|{{getservbyname}}]].
101
102{{ENCODING}} should be a symbol specifying the encoding to be
103used for I/O operations, the default is {{utf-8}}. The encodings for
104the returned in and output ports can be subsequently changed by
105using the {{port-encoding}} setter.
106
107Note: any I/O on the ports returned will not block other running threads.
108
109
110=== tcp-addresses
111
112<procedure>(tcp-addresses PORT)</procedure>
113
114Returns two values for the input- or output-port {{PORT}} (which should be a port returned
115by either {{tcp-accept}} or {{tcp-connect}}): the IP address of the local and the remote
116machine that are connected over the socket associated with {{PORT}}. The returned addresses
117are strings in {{XXX.XXX.XXX.XXX}} notation.
118
119
120=== tcp-port-numbers
121
122<procedure>(tcp-port-numbers PORT)</procedure>
123
124Returns two values for the input- or output-port {{PORT}} (which should be a port returned
125by either {{tcp-accept}} or {{tcp-connect}}): the TCP port numbers of the local and the remote
126machine that are connected over the socket associated with {{PORT}}.
127
128
129=== tcp-abandon-port
130
131<procedure>(tcp-abandon-port PORT)</procedure>
132
133Marks the socket port {{PORT}} as abandoned. This is mainly useful to close down a port
134without breaking the connection.
135
136
137=== tcp-buffer-size
138
139<parameter>tcp-buffer-size</parameter>
140
141Sets the size of the output buffer. By default no output-buffering for
142TCP output is done, but to improve performance by minimizing the
143number of TCP packets, buffering may be turned on by setting this
144parameter to an exact integer greater zero. A buffer size of zero or {{#f}}
145turns buffering off. The setting of this parameter takes effect at the time
146when the I/O ports for a particular socket are created, i.e. when {{tcp-connect}}
147or {{tcp-accept}} is called.
148
149Note that since output is not immediately written to the associated socket, you
150may need to call {{flush-output}}, once you want the output to be transmitted.
151Closing the output port will flush automatically.
152
153=== tcp-read-timeout
154
155<parameter>tcp-read-timeout</parameter>
156
157Determines the timeout for TCP read operations in milliseconds. A timeout of
158{{#f}} disables timeout checking. The default read timeout is 60000, i.e.
1591 minute.
160If timeout occurs while reading, a condition object of kinds {{(exn i/o net timeout)}}
161is thrown.
162
163=== tcp-write-timeout
164
165<parameter>tcp-write-timeout</parameter>
166
167Determines the timeout for TCP write operations in milliseconds. A timeout of
168{{#f}} disables timeout checking. The default write timeout is 60000, i.e.
1691 minute.
170If timeout occurs while writing, a condition object of kinds {{(exn i/o net timeout)}}
171is thrown.
172
173=== tcp-connect-timeout
174
175<parameter>tcp-connect-timeout</parameter>
176
177Determines the timeout for {{tcp-connect}} operations in milliseconds. A timeout of
178{{#f}} disables timeout checking and is the default.
179If timeout occurs while trying to connect, a condition object of kinds {{(exn i/o net timeout)}}
180is thrown.
181
182
183=== tcp-accept-timeout
184
185<parameter>tcp-accept-timeout</parameter>
186
187Determines the timeout for {{tcp-accept}} operations in milliseconds. A timeout of
188{{#f}} disables timeout checking and is the default.
189If timeout occurs while waiting for connections, a condition object of kinds {{(exn i/o net timeout)}}
190is thrown.
191
192
193=== Example
194
195A very simple example follows. Say we have the two files {{client.scm}}
196and {{server.scm}}:
197
198<enscript highlight=scheme>
199; client.scm
200(import (chicken io) (chicken tcp))
201(define-values (i o) (tcp-connect "localhost" 4242))
202(write-line "Good Bye!" o)
203(print (read-line i))
204</enscript>
205
206<enscript highlight=scheme>
207; server.scm
208(import (chicken io) (chicken tcp))
209(define l (tcp-listen 4242))
210(define-values (i o) (tcp-accept l))
211(write-line "Hello!" o)
212(print (read-line i))
213(close-input-port i)
214(close-output-port o)
215</enscript>
216
217 % csc server.scm
218 % csc client.scm
219 % ./server &
220 % ./client
221 Good Bye!
222 Hello!
223
224---
225Previous: [[Module (chicken syntax)]]
226
227Next: [[Module (chicken time)]]